package module.map.mapList
{
	import drongo.model.events.ModelEvent;

	import flash.events.Event;
	import flash.filesystem.File;

	import common.view.FGUIMediator;
	import common.window.AlertDialog;

	import fairygui.GButton;
	import fairygui.event.ItemEvent;

	import module.map.model.MapModel;
	import module.map.model.ProjectModel;
	import module.map.model.Propertys;
	import module.map.newMap.NewMapWindow;

	import robotlegs.bender.extensions.mediatorMap.api.IMediatorMap;

	import ui.Map.UI_MapListPanel;

	public class MapListMediator extends FGUIMediator
	{
		[Inject]
		public var view:UI_MapListPanel;
		[Inject]
		public var project:ProjectModel;
		[Inject]
		public var mediatorMap:IMediatorMap;

		public function MapListMediator()
		{
			super();
		}
		override public function initialize():void
		{
			super.initialize();
			this.AddEvents();
			this.RefreshMapList();
		}

		override public function destroy():void
		{
			this.RemoveEvents();
			super.destroy();
		}

		private function AddEvents():void
		{
			this.view.m_btnAdd.addClickListener(this.NewMapButtonClick);
			this.view.m_btnDelete.addClickListener(this.DeleteButtonClick);
			this.view.m_btnRefresh.addClickListener(this.RefreshButtonClick);
			this.view.m_list.addEventListener(ItemEvent.CLICK, this.MapListItemClick);

			this.project.maps.addEventListener(ModelEvent.ADD_CHILD, this.MapListChanged);
			this.project.maps.addEventListener(ModelEvent.REMOVE_CHILD, this.MapListChanged);
		}

		private function RemoveEvents():void
		{
			this.view.m_btnAdd.removeClickListener(this.NewMapButtonClick);
			this.view.m_btnDelete.removeClickListener(this.DeleteButtonClick);
			this.view.m_btnRefresh.removeClickListener(this.RefreshButtonClick);
			this.view.m_list.removeEventListener(ItemEvent.CLICK, this.MapListItemClick);

			// this.project.maps.removeEventListener(ArrayPropertyEvent.ADD,this.MapListChanged);
			// this.project.maps.removeEventListener(ArrayPropertyEvent.REMOVE,this.MapListChanged);
		}

		private function MapListItemClick(e:ItemEvent):void
		{
			if (this.view.m_list.selectedIndex < 0)
			{
				return;
			}
			var mapData:MapModel = this.project.maps.GetAt(this.view.m_list.selectedIndex) as MapModel;
			if (mapData == null)
			{
				return;
			}
			this.project.currentMap = mapData;
			// 添加进打开列表
			var index:int = this.project.openList.GetChildIndex(mapData);
			if (index < 0)
			{
				this.project.openList.Push(mapData);
			}
		}

		private function MapListChanged(e:ModelEvent):void
		{
			this.RefreshMapList();
		}

		private function RefreshButtonClick(e:Event):void
		{
			this.project.RefreshMapList();
		}

		private function DeleteButtonClick(e:Event):void
		{
			if (this.view.m_list.selectedIndex < 0)
			{
				return;
			}
			var mapData:MapModel = this.project.maps.GetAt(this.view.m_list.selectedIndex) as MapModel;
			if (mapData == null)
			{
				return;
			}
			var mapID:String = mapData.Get(Propertys.ID).GetValue();
			var file:File = new File(this.project.mapsFile.nativePath + File.separator + mapID + ".json");
			if (!file.exists)
			{
				return;
			}
			AlertDialog.open(this.view.root, "删除地图：" + mapID + "将无法还原,是否继续", "继续", "取消", this.DeleteMapCallBack);
		}
		private function DeleteMapCallBack(index:Number):void
		{
			if (index == 0)
			{
				return;
			}
			var mapData:MapModel = this.project.maps.GetAt(this.view.m_list.selectedIndex) as MapModel;
			if (mapData == null)
			{
				return;
			}
			var mapID:String = mapData.Get(Propertys.ID).GetValue();
			// 1.删除配置
			var file:File = new File(this.project.mapsFile.nativePath + File.separator + mapID + ".json");
			file.deleteFile();
			// 2.删除数据
			this.project.maps.Remove(mapData);
			// 取消选择
			this.view.m_list.selectedIndex = -1;
			this.project.currentMap = null;
		}

		private function RefreshMapList():void
		{
			this.view.m_list.removeChildrenToPool();
			if (!this.project.maps == null || this.project.maps.length == 0)
			{
				return;
			}
			var itemData:MapModel;
			var itemRender:GButton;
			for (var index:int = 0; index < this.project.maps.length; index++)
			{
				itemData = this.project.maps.GetAt(index) as MapModel;
				var name:String = itemData.Get(Propertys.NAME).GetValue();
				var id:String = itemData.Get(Propertys.ID).GetValue();
				itemRender = this.view.m_list.addItemFromPool().asButton;
				itemRender.title = id + "[" + name + "]";
				itemRender.tooltips = id;
				itemRender.data = itemData;
			}
		}

		private function NewMapButtonClick(e:Event):void
		{
			var window:NewMapWindow = new NewMapWindow();
			window.showOn(this.view.root);
			window.centerOn(this.view.root);
			mediatorMap.mediate(window);
		}
	}
}